package com.huilian.iotbox.admin.quartz.job;

import com.alibaba.fastjson.JSON;
import com.huilian.iotbox.admin.service.DeviceService;
import com.huilian.iotbox.data.dto.DeviceDto;
import com.huilian.iotbox.data.enums.DeviceOnlineOfflineRecordEnum;
import com.huilian.iotbox.data.service.DeviceCommonService;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import com.huilian.iotbox.data.vo.DeviceVo;
import com.huilian.tool.weixin.service.WXSendMessageService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 盒子自动离线
 *
 * @author chenqiwen
 * @CreateDate: 2021-09-18-13-43
 * @Description:
 */
@Slf4j
public class BoxAutoOffLineJob implements BaseJob {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private DeviceCommonService deviceCommonService;
    @Autowired
    private WXSendMessageService wxSendMessageService;
    @Autowired
    private ErrorLogCommonService errorLogCommonService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("进入-盒子自动离线-定时任务");
        List<DeviceVo> deviceList = deviceService.findAllDeviceByNoHeart(150);

        DeviceDto device = null;
        for (DeviceVo item : deviceList) {
            try {
                if (item.getIsBluetooth()) {
                    // 蓝牙设备默认在线
                    continue;
                }
                device = new DeviceDto();
                device.setNetworkIntensity(0);
                device.setId(item.getId());

                deviceService.updateDeviceById(device);
                wxSendMessageService.sendOfflineMessage(item.getDeviceNo(), item.getStoreName(), item.getId(), item.getUserId(), item.getMerchantStoreId());
            } catch (Exception e) {
                e.printStackTrace();
                errorLogCommonService.writerInfoException(device.getDeviceNo() + "盒子自动离线出错", device, e);
            } finally {
                item.setStartDate(new Date());
                item.setDeviceOnlineOfflineRecordType(DeviceOnlineOfflineRecordEnum.OFFLINE.getCode());

                final Integer serviceProvider = deviceCommonService.getServiceProvider(item.getDeviceNo());
                CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
                switch (serviceProvider) {
                    case 1:
                        rabbitTemplate.convertAndSend("iotBox-deviceOnlineOrOffline-1", "iotBox-deviceOnlineOrOffline.1", JSON.toJSONString(item), correlationData);
                        break;
                    case 2:
                        rabbitTemplate.convertAndSend("iotBox-deviceOnlineOrOffline-2", "iotBox-deviceOnlineOrOffline.2", JSON.toJSONString(item), correlationData);
                        break;
                }
            }
        }
    }
}
